package com;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyHttpServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        //增加防盗链，判断访问来源url是否是同域的
//        String referer=req.getHeader("Referer");
//        System.out.println("referer:"+referer);
//        if(referer==null||!referer.contains("localhost:8080/index.jsp")){
//            resp.sendRedirect("/index.jsp");
//            return;
//        }
        resp.setCharacterEncoding("UTF-8");
//增加网头
        req.getRequestDispatcher("/Head").include(req, resp);

        resp.getWriter().write("hello,方晶"+"\n");
//增加网尾
        req.getRequestDispatcher("/Foot").include(req, resp);

        System.out.println("contextPath:" + req.getContextPath());
        // System.out.println("referer:"+referer);
        System.out.println("requestURL:" + req.getRequestURL());
        System.out.println("remoteAddr:" + req.getRemoteAddr());

        //设置request字符编码的格式
        req.setCharacterEncoding("UTF-8");

//通过html的name属性，获取到值
        String username = req.getParameter("username");

        //针对get请求来的中文参数进行解码
        //1、以url方式来的get请求中的中文已经被tomcat按默认的ISO方式编码了
        //2、需要先通过ISO解码为原始数据，再通过UTF-8进行编码显示
        byte[] bytes = username.getBytes("ISO8859-1");
        String username1 = new String(bytes, "UTF-8");


        String password = req.getParameter("password");
        String gender = req.getParameter("gender");

//复选框和下拉框有多个值，获取到多个值
        String[] hobbies = req.getParameterValues("hobbies");
        String[] address = req.getParameterValues("address");

//获取到文本域的值
        String description = req.getParameter("textarea");

//得到隐藏域的值
        String hiddenValue = req.getParameter("aaa");

        System.out.println(username);
        System.out.println(username1);
        System.out.println(password);
        System.out.println(gender);

        System.out.println(hobbies);
        System.out.println(address);
        System.out.println(description);
        System.out.println(hiddenValue);


    }
}
